package org.jeeasy.generate.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.jeeasy.common.core.annotation.dict.DictTranslation;
import org.jeeasy.common.core.base.SimpleCurdController;
import org.jeeasy.common.core.domain.model.QueryPageModel;
import org.jeeasy.common.core.domain.vo.R;
import org.jeeasy.generate.domain.GenTable;
import org.jeeasy.generate.service.GenTableService;
import org.springframework.web.bind.annotation.*;

/**
 * @author AlpsDDJ
 * @date 2020/11/9
 */
@RestController
@Tag(name = "代码生成-表信息")
@RequestMapping("/gen/table")
public class GenTableController extends SimpleCurdController<GenTableService, GenTable> {

    @GetMapping
    @DictTranslation
    @Operation(summary = "表信息列表")
    public R<IPage<GenTable>> list(QueryPageModel queryPageModel) {
        //R<IPage<GenTable>> page = super.queryPage(queryPageModel, req);
        IPage<GenTable> page = service.page(queryPageModel.getPage(), getWrapper());
        page.getRecords().forEach(item -> {
            service.setFields(item);
        });
        return R.ok(page);
    }

    @GetMapping("/{id}")
    @DictTranslation
    @Operation(summary = "根据ID查找表信息")
    public R<GenTable> info(@PathVariable("id") String id) {
        return R.ok(super.getById(id));
    }

    @PutMapping
    @Operation(summary = "编辑表信息")
    public R<Void> edit(@RequestBody GenTable entity) {
        service.updateWithFields(entity);
        return R.ok();
    }

    @PostMapping
    @Operation(summary = "添加表信息")
    public R<Void> add(@RequestBody GenTable entity) {
        service.saveWithFields(entity);
        return R.ok();
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "根据ID删除表信息")
    public R<Void> remove(@PathVariable("id") String id) {
        super.deleteById(id);
        return R.ok();
    }

    @DeleteMapping("/batch")
    @Operation(summary = "批量删除表信息")
    public R<Void> batchRemove(@RequestParam(name = "ids") String ids) {
        super.batchDelete(ids);
        return R.ok();
    }

}
